<?php
namespace app\pay\controller;

use wxuserinfo\Wxuserinfo as UserinfoClass;
use think\Controller;
use think\Request;
use think\Db;

class Wxuserinfo extends Controller
{
    //只获取openid作为用户的唯一标识
    public function base(){
        $params = Request::instance()->param();
        $params['scope'] = 'snsapi_base';
        $UserinfoClass = new UserinfoClass();
        if(isset($params['return_url'])){
            if(!isset($params['code']))
            {
                $UserinfoClass -> index($params);
                exit;
            }
            else{
                $res = $UserinfoClass -> requireCode($params);
                //查询数据库是否有此信息。
                if(!isset($res['errcode'])){
                    //查询数据库是否有此信息。
                    //无此信息时注册 否则登录。
                    $url = $params['return_url'] .'?openid='.$res['openid'];
                    Header("Location: $url");
                }
                else{
                    $data = ['code'=>0,'msg'=>$res['errmsg']];
                    echo json_encode($data);
                    exit;
                }
            }
        }
        else{
            $data =['code'=>0,'msg'=>'缺少参数return_url'];
            echo json_encode($data);
            exit;
        }
    }
    //传输回调地址
    //用户注册/授权登录/获取用户全部信息
    public function userinfo(){
        $params = Request::instance()->param();
        $params['scope'] = 'snsapi_userinfo';
        $UserinfoClass = new UserinfoClass();
        if(isset($params['return_url'])){
            if(!isset($params['code']))
            {
                $UserinfoClass -> index($params);
                exit;
            }
            else{
               $res = $UserinfoClass -> requireCode($params);
               //查询数据库是否有此信息。
                if(!isset($res['errcode'])){
                    //查询数据库是否有此信息。
                    //无此信息时注册 否则登录。
                    $search = Db::name('user')->where('open_id',$res['openid'])->find();
                    if(!$search){
                        if($res['sex']==1){
                            $sex = '男';
                        }
                        else{
                            $sex = '女';
                        }
                        $data=[
                            'open_id'=>$res['openid'],
                            'user_name'=>$res['nickname'],
                            'user_sex' =>$sex,
                            'user_icon'=>$res['headimgurl']
                        ];
                        Db::name('user')->insert($data);
                    }
                    $url = $params['return_url'] .'?openid='.$res['openid'];
                    $this->assign('url',$url);
                    return $this->fetch();
                }
                else{
                    $data = ['code'=>0,'msg'=>$res['errmsg']];
                    echo json_encode($data);
                    exit;
                }
            }
        }
        else{
            $data =['code'=>0,'msg'=>'缺少参数return_url'];
            echo json_encode($data);
            exit;
        }
    }
    //前端获取code的情况。
    public function userinfoJson(){
        $params = Request::instance()->param();
        if(isset($params['scope'])&&isset($params['code'])){
            $UserinfoClass = new UserinfoClass();
            $res = $UserinfoClass -> requireCode($params);
            if(!isset($res['errcode'])){
                //写入数据库，并返回给前端数据.
                $data = ['code'=>1,'msg'=>'获取成功！','data'=>$res];
            }
            else{
                $data = ['code'=>0,'msg'=>'错误！','data'=>$res];
            }
        }
        else{
            $data = ['code'=>0,'msg'=>'参数缺失！','data'=>[]];
        }
        return json_encode($data);
    }

}